﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableParsers;

namespace Speakup
{
    public static class AttendencyAssigner
    {
        //assigns theme to all applicable classes
        //assigns 'attended' status to all classes
        //assigns 'attended' status to all weekThemes
        static public void AssignAttendedToClasses(ClassList classes, PersonalStats stats)
        {
            Dictionary<WeekThemeKey, List<Class>> themeKeyDict = new Dictionary<WeekThemeKey, List<Class>>();
            List<ThemeLevel> supportedLevels = WeekTheme.SupportedThemeLevels();

            ThemeLevel currentLevel = (from n in supportedLevels
                                       where n.ContainsLevel(stats.Level)
                                       select n).First();
            
            foreach (Class c in classes)
            {
                int week = Week.GetWeek(c.ClassTime.Date);

                WeekThemeKey key = new WeekThemeKey(c.ClassName.Type, currentLevel, week);

                if(!themeKeyDict.Keys.Contains(key))
                {
                    themeKeyDict[key] = new List<Class>();
                }

                themeKeyDict[key].Add(c);
                c.Theme = null;
                c.ThemeAttended = false;
            }

            WeekTheme.ThemeFilter filter = WeekTheme.LevelFilter(currentLevel);
            List<WeekTheme> themes = WeekTheme.GetWeekThemes(filter);

            AssignAttendedToWeeks(themes, stats);

            foreach (WeekTheme weekTheme in themes )
            {
                WeekThemeKey key = new WeekThemeKey(weekTheme.ClassType, currentLevel, weekTheme.Week);

                if (themeKeyDict.Keys.Contains(key))
                {
                    foreach (Class c in themeKeyDict[key])
                    {
                        c.Theme = weekTheme.Theme;
                        c.ThemeAttended = weekTheme.Attended;
                    }
                }

            }
        }

        static public void AssignAttendedToWeeks(List<WeekTheme> weekThemes, PersonalStats stats)
        {
            List<ThemeLevel> supportedLevels = WeekTheme.SupportedThemeLevels();

            List<WeekThemeKey> attendedThemes = new List<WeekThemeKey>();
            foreach (var c in stats.AttendedClasses)
            {
                if (c.Present)
                {
                    int week = Week.GetWeek(c.ClassTime.Date);
                    int level = Level.Unit2Level(c.Unit);
                    ThemeLevel themeLevel = (from n in supportedLevels
                                             where n.ContainsLevel(level)
                                             select n).First();

                    attendedThemes.Add(new WeekThemeKey(c.ClassName.Type, themeLevel, week));
                }
            }

            foreach (var w in weekThemes)
            {
                if (attendedThemes.Contains(w.Key))
                {
                    w.Attended = true;
                }
                else
                {
                    w.Attended = false;
                }
            }
        }
    }
}
